package com.hc.acoustics.ui.activity

import android.content.Intent
import android.os.Bundle
import com.hc.acoustics.base.BaseActivity
import com.yc.yclibrary.base.YcAppCompatActivity
import kotlinx.android.synthetic.main.web_activity.*
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.view.KeyEvent
import android.view.View


/**
 *  浏览器
 */
class WebActivity : BaseActivity() {
    val url = "http://mp.weixin.qq.com/s?__biz=Mzg3NzE2MDU5Mg==&mid=100000962&idx=1&sn=809423ef0e4ae8846bc8d03dae111d33&chksm=4f26753d7851fc2b17c9108fec78035e3b7c5794e44e07380f7224a0c831f0be0bca82f158fd#rd"

    companion object {
        private const val KEY_TITLE = "key_title"
        @JvmStatic
        fun newInstance(activity: YcAppCompatActivity, title: String) {
            val intent = Intent(activity, WebActivity::class.java)
            intent.putExtra(KEY_TITLE, title)
            activity.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return com.hc.acoustics.R.layout.web_activity
    }

    override fun initView(savedInstanceState: Bundle?) {
        setActionBar(intent.getStringExtra(KEY_TITLE))
        webView.setWebViewClient(object : WebViewClient() {
            override fun onPageFinished(view: WebView, url: String) {
                progressBar.setVisibility(View.GONE)
            }
        })
        webView.getSettings().setJavaScriptEnabled(true)
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW)
        }
        //去掉缩放按钮
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            webView.getSettings().setBuiltInZoomControls(true)
            webView.getSettings().setDisplayZoomControls(false)
        }
        webView.getSettings().setUseWideViewPort(true)//是否使用自适应
        webView.getSettings().setLoadWithOverviewMode(true)
        webView.loadUrl(url)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}